import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useUserStore } from '@/stores/user'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/LoginView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/RegisterView.vue'),
      meta: { requiresAuth: false },
    },
    {
      path: '/',
      component: () => import('@/layouts/MainLayout.vue'),
      meta: { requiresAuth: true },
      children: [
        {
          path: 'home',
          name: 'home',
          component: () => import('@/views/HomeView.vue'),
        },
        {
          path: 'users',
          name: 'users',
          component: () => import('@/views/UsersView.vue'),
        },
        {
          path: 'sync',
          name: 'sync',
          component: () => import('@/views/SyncView.vue'),
        },
        {
          path: 'souls',
          name: 'souls',
          component: () => import('@/views/SoulsView.vue'),
        },
        {
          path: 'souls/:id',
          name: 'soul-details',
          component: () => import('@/views/SoulDetailsView.vue'),
        },
        {
          path: 'souls/:id/edit',
          name: 'soul-edit',
          component: () => import('@/views/SoulEditView.vue'),
        },
        {
          path: 'judgments',
          name: 'judgments',
          component: () => import('@/views/JudgmentsView.vue'),
        },
        {
          path: 'judgments/new',
          name: 'judgment-new',
          component: () => import('@/views/JudgmentEditView.vue'),
        },
        {
          path: 'judgments/:id',
          name: 'judgment-details',
          component: () => import('@/views/JudgmentDetailsView.vue'),
        },
        {
          path: 'judgments/:id/edit',
          name: 'judgment-edit',
          component: () => import('@/views/JudgmentEditView.vue'),
        },
        {
          path: 'reincarnation',
          name: 'reincarnation',
          component: () => import('@/views/ReincarnationView.vue'),
        },
        {
          path: 'reincarnation/new',
          name: 'reincarnation-new',
          component: () => import('@/views/ReincarnationEditView.vue'),
        },
        {
          path: 'reincarnation/:id',
          name: 'reincarnation-details',
          component: () => import('@/views/ReincarnationDetailsView.vue'),
        },
        {
          path: 'reincarnation/:id/edit',
          name: 'reincarnation-edit',
          component: () => import('@/views/ReincarnationEditView.vue'),
        },
        {
          path: 'hell',
          name: 'hell',
          component: () => import('@/views/HellView.vue'),
        },
        {
          path: 'hell/new',
          name: 'hell-new',
          component: () => import('@/views/HellEditView.vue'),
        },
        {
          path: 'hell/:id',
          name: 'hell-details',
          component: () => import('@/views/HellDetailsView.vue'),
        },
        {
          path: 'hell/edit/:id',
          name: 'hell-edit',
          component: () => import('@/views/HellEditView.vue'),
        },
        {
          path: 'reports',
          name: 'reports',
          component: () => import('@/views/ReportsView.vue'),
        },
        {
          path: 'currency',
          name: 'currency',
          component: () => import('@/views/CurrencyView.vue'),
        },
        {
          path: 'logs',
          name: 'logs',
          component: () => import('@/views/LogsView.vue'),
        },
        {
          path: 'profile',
          name: 'profile',
          component: () => import('@/views/ProfileView.vue'),
        },
        {
          path: 'system',
          name: 'System',
          component: () => import('@/layouts/MainLayout.vue'),
          meta: {
            title: '系统管理',
            icon: 'settings',
            requiresAuth: true,
          },
          children: [
            {
              path: 'roles',
              name: 'roles',
              component: () => import('@/views/system/RoleView.vue'),
            },
            {
              path: 'permissions',
              name: 'permissions',
              component: () => import('@/views/system/PermissionView.vue'),
            },
            {
              path: 'settings',
              name: 'settings',
              component: () => import('@/views/system/SettingsView.vue'),
            },
          ],
        },
      ],
    },
  ],
})

router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth)

  if (requiresAuth && !userStore.isLoggedIn) {
    next('/login')
  } else {
    next()
  }
})

export default router
